package com.heima.wemedia.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.wemedia.dtos.PageRequestSensitiveDto;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.DateUtils;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Override
    public void addSensitive(SensitiveDto dto) {
        // 1. 校验参数
        if (dto.getSensitives()==null || dto.getSensitives().length()==0 ){
            throw new RuntimeException("添加敏感词失败,敏感词为空");
        }
        //2. 检查违禁词是否已经存在
        Integer count = lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()).count();
        if (count > 0){
            throw new RuntimeException("添加敏感词失败,敏感词已存在;");
        }
        //3. 添加违禁词, 补全创建时间
        WmSensitive wmSensitive = new WmSensitive();
        wmSensitive.setSensitives(dto.getSensitives());
        String currentDateTime = DateUtils.getCurrentDateTime("yyyy-MM-dd HH:mm:ss");
        Date date = DateUtils.stringToDate(currentDateTime, "yyyy-MM-dd HH:mm:ss");
        wmSensitive.setCreatedTime(date);
        save(wmSensitive);
        //这里只写能保存的逻辑,数据库中已存在该违禁词仍不返回值,都视为成功
    }

    @Override
    public void updateSensitive(SensitiveDto dto) {
        //1. 校验参数
        if (StrUtil.isBlank(dto.getId()) || StrUtil.isBlank(dto.getSensitives())){
            throw new RuntimeException("更新敏感词失败, id或name为空");
        }
        //2. 检查违禁词是否已经存在
        WmSensitive sensitive = lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()).one();
        if (sensitive != null){
            throw new RuntimeException("更新敏感词失败, 敏感词已存在");
        }
        //3. 更新违禁词
        lambdaUpdate().set(WmSensitive::getSensitives,dto.getSensitives()).eq(WmSensitive::getId,dto.getId()).update();
    }

    @Override
    public void deleteSensitiveById(Long id) {
        //1. 检查参数
        if (id == null){
            throw new RuntimeException("删除敏感词失败,id为空");
        }
        //2. 判断id是否存在
        if (!(lambdaQuery().eq(WmSensitive::getId,id).count() > 0)){
            throw new RuntimeException("删除敏感词失败,id不存在");
        }
        //3. 删除敏感词
        removeById(id);
    }

    @Override
    public PageResponseResult<WmSensitive> pageConditionQuery(PageRequestSensitiveDto dto) {
        //1. 检查参数
        if (dto.getPage() == null || dto.getSize() == null){
            throw new RuntimeException("分页查询敏感词失败,参数部分为空");
        }
        //2. 检查分页条件
        if (!(dto.getPage() > 0) || !(dto.getSize() > 0)){
            throw new RuntimeException("分页查询敏感词失败,分页条件不合法");
        }
        //3. 构建查询参数和分页参数
        //4. 查询结果
        Page<WmSensitive> page = lambdaQuery()
                .like(StrUtil.isBlank(dto.getName()), WmSensitive::getSensitives, dto.getName())
                .page(new Page<WmSensitive>(dto.getPage(), dto.getSize()));
        //5. 封装返回值
        PageResponseResult<WmSensitive> pageResult = new PageResponseResult<>();
        pageResult.setData(page.getRecords());
        pageResult.setCurrentPage((int) page.getCurrent());
        pageResult.setSize((int)page.getSize());
        pageResult.setTotal((int)page.getTotal());
        return pageResult;

    }
}
